/*
 * Copyright 2011 Philippe Blanc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package me.l1k3.core.client;

import me.l1k3.core.client.inter.ComputedStyleProperty;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.regexp.shared.RegExp;

public class Core {
    private static EventBus bus;
    
    public final static String REGEX_PIXEL = "^\\d+(px)?$";
    public final static String REGEX_POINT = "^\\d+(pt)?$";
    public final static String REGEX_PERCENTAGE = "^\\d+(%)?$";
    
	private static ComputedStyleProperty computedStyleProperty;
	
	protected Core() { }
	
    public final static EventBus getEventBus() {
        if(Core.bus==null) {
            Core.bus = new SimpleEventBus();
        }
        
        return Core.bus;
    }
    
    public final static void setEventBus(EventBus sharedBus) throws Core.EventBusNoNullException {
        if(Core.bus!=null) {
            throw new Core.EventBusNoNullException();
        }
        
        Core.bus = sharedBus;
    }
	
    public final static boolean isUnitPoint(String property) {
        RegExp exp = RegExp.compile(REGEX_POINT);
        return exp.test(property);
    }
    
	public final static boolean isUnitPixel(String property) {
	    RegExp exp = RegExp.compile(REGEX_PIXEL);
	    return exp.test(property);
	}
	
	public final static boolean isUnitPercentage(String property) {
        RegExp exp = RegExp.compile(REGEX_PERCENTAGE);
        return exp.test(property);
    }
	
	public final static double getOuterWidth(Element element) {
	    return element.getOffsetWidth()+Core.getComputedStylePropertyAsPixel(element, "margin-left")+Core.getComputedStylePropertyAsPixel(element, "margin-right");
	}
	
	public final static double getOuterHeight(Element element) {
        return element.getOffsetHeight()+Core.getComputedStylePropertyAsPixel(element, "margin-top")+Core.getComputedStylePropertyAsPixel(element, "margin-bottom");
    }
	
	public final static String getComputedStyleProperty(Element element, String property) {
	    return getComputedStyleProperty().getComputedStyleProperty(element, property);
	}
	
	public final static double getComputedStylePropertyAsPixel(Element element, String property) {
	    return getComputedStyleProperty().getComputedStylePropertyAsPixel(element, property);
	}

	public final static boolean elementIsVisible(Element element) {
		if (Core.getComputedStyleProperty(element, "visibility").equalsIgnoreCase("hidden")) {
			return false;
		}

		if (Core.getComputedStyleProperty(element, "display").equalsIgnoreCase("none")) {
			return false;
		}
		
		return true;
	}
	
	public final static boolean pathIsVisible(Element element) {
		while(element != null) {
			if (Core.getComputedStyleProperty(element, "visibility").equalsIgnoreCase("hidden")) {
				return false;
			}

			if (Core.getComputedStyleProperty(element, "display").equalsIgnoreCase("none")) {
				return false;
			}
		
			element = element.getParentElement();
		}
		
		return true;
	}
	
	@Deprecated
	public final static void setOpacity(Element e, double opacity) {
	    e.getStyle().setOpacity(opacity);
	}
	
	public final static native int createTimeout(JavaScriptObject handleFire, int delay) /*-{
       return $wnd.setTimeout(handleFire, delay);
    }-*/;

    public final static native String toCamelCase(String string) /*-{
        return string.replace(/(\-[a-z])/g, function(c){return c.toUpperCase().replace('-','');});
    }-*/;
    
    public final static native String camelCaseToDash(String string) /*-{
        return string.replace(/([A-Z])/g, function(c){return "-"+c.toLowerCase();});
    }-*/;
    
    public final static native int parseInt(String value) /*-{
        return parseInt(value);
    }-*/;
    
    public final static native double parseDouble(String value) /*-{
        return parseFloat(value);
    }-*/;
    
    public final static native double parseDouble(String value, int resolution) /*-{
        var r = 1;
        
        for(var i=0;i<resolution;i++) {
            r = r*10;
        }
        
        return Math.round(parseFloat(value)*r)/r;
    }-*/;
    
    protected final static ComputedStyleProperty getComputedStyleProperty() {
        if(computedStyleProperty==null) {
            computedStyleProperty = GWT.create(ComputedStyleProperty.class);
        }
        
        return computedStyleProperty;
    }
    
    @SuppressWarnings("serial")
    public final static class EventBusNoNullException extends Exception { }
}
